#! /bin/bash -e

#####################################################################
#                                                                   #
#           Compiles Faust programs to JUCE standalone or plugin    #
#           (c) Grame, 2016-2021                                    #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags
. usage.sh

CXXFLAGS+=" $MYGCCFLAGS"  # So that additional CXXFLAGS can be used

DEBUG=false

echoHelp()
{
    usage faust2juce "[options] [Faust options] <file.dsp>"
    require Juce
    echo "Compiles Faust programs to JUCE standalones or plugins (see https://github.com/grame-cncm/faust/tree/master-dev/architecture/juce)"
    option
    options -osc -midi -soundfile
    option "-nvoices <num>"
    option "-effect <effect.dsp>"
    option "-effect auto"
    option -standalone "to produce a standalone project, otherwise a plugin project is generated"
    option -"vst2sdkdir <folder>" "to set directory to VST 2 SDK."
    option -"jucemodulesdir <folder>" "to set JUCE modules directory to <folder>, such as ~/JUCE/modules"
    option -disable-splash-screen "to disable the JUCE splash screen (license is required)."
    option -jsynth "to use JUCE polyphonic Synthesizer instead of Faust polyphonic code"
    option -llvm "to use the LLVM compilation chain (OSX and Linux for now)"
    option -magic "to generate a project using the PluginGuiMagic GUI builder"
    option "Faust options"
    exit
}

if [ "$#" -eq 0 ]; then
    echo 'Please, provide a Faust file to process !'
    echo ''
    echoHelp
fi

#-------------------------------------------------------------------
#PHASE 1 : dispatch command arguments
#-------------------------------------------------------------------

OSCLIB=""
POLY="POLY"
DEF=""
EFFECT=""
NVOICES=-1
STANDALONE="0"
IS_SYNTH="0"
IS_MIDI="0"
IS_LLVM="0"
JUCE_POLY="0"
FAUSTFLOAT="float"
JUCER=""
VST2_SDK=""  # The directory containing "plugininterfaces/vst2.x/aeffect.h"
BUILD_VST2="0"  # By default, don't build VST2
JUCE_MODULES_DIR="\.\.\/\.\.\/modules"  # By default, the generated folder is supposed to be copied in JUCE/examples folder
PLUGIN_FORMATS="buildAU,buildAUv3,buildVST3,buildStandalone"  # can append "buildVST"
PLUGIN_CHARACTERISTICS=""
DISPLAY_SPLASH_SCREEN="1"

while [ $1 ]
do
    p=$1

    if [ $p = "--help" ] || [ $p = "-help" ] || [ $p = "-h" ]; then
        echoHelp
    fi

    if [ "$p" = -debug ]; then
        DEBUG=true
    elif [ $p = "-nvoices" ]; then
        shift
        IS_SYNTH="1"
        NVOICES=$1
    elif [ $p = "-standalone" ]; then
        STANDALONE="1"
    elif [ $p = "-effect" ]; then
        DEF+="POLY2 "
        POLY="POLY2"
        shift
        EFFECT=$1
    elif [ $p = "-vst2sdkdir" ]; then
        shift
        VST2_SDK=$(echo $1 | sed -e 's/\//\\\//g')
        if [ $VST2_SDK != "" ]; then
            PLUGIN_FORMATS+=",buildVST"
            BUILD_VST2="1"
        fi
    elif [ $p = "-jucemodulesdir" ]; then
        shift
        JUCE_MODULES_DIR=$(echo $1 | sed -e 's/\//\\\//g')
    elif [ $p = "-disable-splash-screen" ]; then
        DISPLAY_SPLASH_SCREEN="0"
    elif [ $p = "-jsynth" ]; then
        DEF+="JUCE_POLY "
        JUCE_POLY="1"
    elif [ $p = "-midi" ]; then
        DEF+="MIDICTRL "
        IS_MIDI="1"
    elif [ $p = "-osc" ]; then
        DEF+="OSCCTRL "
    elif [ $p = "-llvm" ]; then
        IS_LLVM="1"
    elif [ $p = "-magic" ]; then
        DEF+="PLUGIN_MAGIC "
    elif [ $p = "-soundfile" ]; then
        DEF+="SOUNDFILE "
    elif [ ${p:0:1} = "-" ]; then
        if [ $p = "-double" ]; then
            FAUSTFLOAT="double"
        fi
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi

shift

done

# configure JUCER file

if [ $STANDALONE = "1" ]; then
    if [ $IS_LLVM = "1" ]; then
        JUCER="standalone-llvm.jucer"
    else
        JUCER="standalone.jucer"
    fi
else
    if [ $IS_LLVM = "1" ]; then
        JUCER="plugin-llvm.jucer"
    else
        JUCER="plugin.jucer"
    fi
fi

if [ $STANDALONE = "1" ] && [ $JUCE_POLY = "1" ]; then
    echo "Cannot use -standalone with -jsynth"
    exit
fi

if [ $POLY = "POLY2" ] && [ $JUCE_POLY = "1" ]; then
    echo "Cannot use -effect with -jsynth"
    exit
fi

# IS_SYNTH may have previously been set to "1" by the "-nvoices" command line option
if [ "$IS_SYNTH" == "0" ]; then
    # look for polyphonic "nvoices" metadata in the DSP file
    if [ $(cat $FILES | grep -c "nvoices:") -ne 0 ] || [ $(cat $FILES | grep -c "declare nvoices") -ne 0 ]
    then
        # a variation of the string "nvoices" was found
        IS_SYNTH="1"
    fi
fi

# Set PLUGIN_CHARACTERISTICS, and the order matters.
if [ $IS_SYNTH = "1" ]; then
    PLUGIN_CHARACTERISTICS+=",pluginIsSynth"
fi
if [ $IS_MIDI = "1" ]; then
    PLUGIN_CHARACTERISTICS+=",pluginProducesMidiOut,pluginWantsMidiIn"
fi
# Don't allow PLUGIN_CHARACTERISTICS to start with a comma
if [ ${PLUGIN_CHARACTERISTICS::1}="," ]; then
    PLUGIN_CHARACTERISTICS="${PLUGIN_CHARACTERISTICS:1}"
fi

#-------------------------------------------------------------------
# compile the *.dsp files
#-------------------------------------------------------------------

for p in $FILES; do

    CUR=$(pwd)
    f=$(basename "$p")
    SRCDIR=$(dirname "$p")

    # creates the dir
    dspName="${f%.dsp}"
    SUB_TYPE=$(shasum $p)
    SUB_TYPE=${SUB_TYPE:0:4}
    rm -rf "$SRCDIR/$dspName"

    if [ $STANDALONE = "0" ]; then
        cp -r "$FAUSTARCH/juce/plugin" "$SRCDIR/$dspName/"
        # setting plugin name to match the dsp
        sed -e "s/SUB_TYPE/$SUB_TYPE/g" "$SRCDIR/$dspName/$JUCER" >> "$SRCDIR/$dspName/$dspName-temp.jucer"
    else
        cp -r "$FAUSTARCH/juce/standalone" "$SRCDIR/$dspName/"
        # setting project name to match the dsp
        sed -e "s/ProjectTitle/$dspName/g" "$SRCDIR/$dspName/$JUCER" >> "$SRCDIR/$dspName/$dspName-temp.jucer"
    fi

    # setting the preprocessing definitions
    sed -e "s/PreProcDef/$DEF/g" "$SRCDIR/$dspName/$dspName-temp.jucer" >> "$SRCDIR/$dspName/$dspName-temp0.jucer"
    sed -e "s/APPL_NAME/$dspName/g" "$SRCDIR/$dspName/$dspName-temp0.jucer" >> "$SRCDIR/$dspName/$dspName-temp1.jucer"

    # MIDI
    sed -e "s/IS_MIDI/$IS_MIDI/g" "$SRCDIR/$dspName/$dspName-temp1.jucer" >> "$SRCDIR/$dspName/$dspName-temp2.jucer"

    # SYNTH
    sed -e "s/IS_SYNTH/$IS_SYNTH/g" "$SRCDIR/$dspName/$dspName-temp2.jucer" >> "$SRCDIR/$dspName/$dspName-temp3.jucer"

    # FAUSTFLOAT
    sed -e "s/FAUST_FLOAT/$FAUSTFLOAT/g" "$SRCDIR/$dspName/$dspName-temp3.jucer" >> "$SRCDIR/$dspName/$dspName-temp4.jucer"

    # JUCE_MODULES_DIR
    sed -e "s/JUCE_MODULES_DIR_DEFAULT/$JUCE_MODULES_DIR/g" "$SRCDIR/$dspName/$dspName-temp4.jucer" >> "$SRCDIR/$dspName/$dspName-temp5.jucer"

    # VST2_SDK
    sed -e "s/VST2_SDK_DEFAULT/$VST2_SDK/g" "$SRCDIR/$dspName/$dspName-temp5.jucer" >> "$SRCDIR/$dspName/$dspName-temp6.jucer"
    sed -e "s/BUILD_VST2_DEFAULT/$BUILD_VST2/g" "$SRCDIR/$dspName/$dspName-temp6.jucer" >> "$SRCDIR/$dspName/$dspName-temp7.jucer"

    # PLUGIN_FORMATS
    sed -e "s/PLUGIN_FORMATS_DEFAULT/$PLUGIN_FORMATS/g" "$SRCDIR/$dspName/$dspName-temp7.jucer" >> "$SRCDIR/$dspName/$dspName-temp8.jucer"

    # PLUGIN_CHARACTERISTICS
    sed -e "s/PLUGIN_CHARACTERISTICS_DEFAULT/$PLUGIN_CHARACTERISTICS/g" "$SRCDIR/$dspName/$dspName-temp8.jucer" >> "$SRCDIR/$dspName/$dspName-temp9.jucer"

    # DISPLAY_SPLASH_SCREEN
    sed -e "s/DISPLAY_SPLASH_SCREEN_DEFAULT/$DISPLAY_SPLASH_SCREEN/g" "$SRCDIR/$dspName/$dspName-temp9.jucer" >> "$SRCDIR/$dspName/$dspName-temp10.jucer"

    # possibly set NVOICES value
    if [ $NVOICES -ge 0 ]; then
        sed -e "s/NUM_VOICES/$NVOICES/g" "$SRCDIR/$dspName/$dspName-temp10.jucer" >> "$SRCDIR/$dspName/$dspName.jucer"
    else
        cp "$SRCDIR/$dspName/$dspName-temp10.jucer" "$SRCDIR/$dspName/$dspName.jucer"
    fi

    # standalone or plugin mode
    if [ $STANDALONE = "0" ]; then
        rm "$SRCDIR/$dspName/plugin-llvm.jucer" "$SRCDIR/$dspName/plugin.jucer"
    else
        rm "$SRCDIR/$dspName/standalone-llvm.jucer" "$SRCDIR/$dspName/standalone.jucer"
    fi

    rm "$SRCDIR/$dspName/$dspName-temp.jucer"  "$SRCDIR/$dspName/$dspName-temp0.jucer"
    rm "$SRCDIR/$dspName/$dspName-temp1.jucer" "$SRCDIR/$dspName/$dspName-temp2.jucer"
    rm "$SRCDIR/$dspName/$dspName-temp3.jucer" "$SRCDIR/$dspName/$dspName-temp4.jucer"
    rm "$SRCDIR/$dspName/$dspName-temp5.jucer" "$SRCDIR/$dspName/$dspName-temp6.jucer"
    rm "$SRCDIR/$dspName/$dspName-temp7.jucer" "$SRCDIR/$dspName/$dspName-temp8.jucer"
    rm "$SRCDIR/$dspName/$dspName-temp9.jucer" "$SRCDIR/$dspName/$dspName-temp10.jucer"

    # standalone of plugin mode
    if [ $STANDALONE = "0" ]; then
        if [ $IS_LLVM = "1" ]; then
            faust -inj "$FAUSTINC/faust/dsp/llvm-dsp-adapter.h" -uim -i -a "$FAUSTARCH/juce/juce-plugin.cpp" $OPTIONS "$SRCDIR/$f" -o "$SRCDIR/$dspName/FaustPluginProcessor.cpp" || exit
            dynamic-faust $OPTIONS "$SRCDIR/$f" -o "$SRCDIR/$dspName/dynamic.o"
        else
            faust -scn base_dsp -uim -i -a "$FAUSTARCH/juce/juce-plugin.cpp" $OPTIONS "$SRCDIR/$f" -o "$SRCDIR/$dspName/FaustPluginProcessor.cpp" || exit
        fi
    else
        if [ $IS_LLVM = "1" ]; then
            faust -inj "$FAUSTINC/faust/dsp/llvm-dsp-adapter.h" -i -a "$FAUSTARCH/juce/juce-standalone.cpp" $OPTIONS "$SRCDIR/$f" -o "$SRCDIR/$dspName/FaustAudioApplication.cpp" || exit
            dynamic-faust $OPTIONS "$SRCDIR/$f" -o "$SRCDIR/$dspName/dynamic.o"
        else
            faust -scn base_dsp -i -a "$FAUSTARCH/juce/juce-standalone.cpp" $OPTIONS "$SRCDIR/$f" -o "$SRCDIR/$dspName/FaustAudioApplication.cpp" || exit
        fi
    fi

    if [ $POLY = "POLY2" ]; then
        if [ $EFFECT = "auto" ]; then
            cat > "$SRCDIR/$dspName/effect.dsp" << EndOfCode
            adapt(1,1) = _;
            adapt(2,2) = _,_;
            adapt(1,2) = _ <: _,_;
            adapt(2,1) = _,_ :> _;
            adaptor(F,G) = adapt(outputs(F),inputs(G));
            process = adaptor(library("$SRCDIR/$f").process, library("$SRCDIR/$f").effect) : library("$SRCDIR/$f").effect;
EndOfCode
            faust -scn base_dsp -i -cn effect -a minimal-effect.cpp "$SRCDIR/$dspName/effect.dsp" -o "$SRCDIR/$dspName/effect.h" || exit
            rm "$SRCDIR/$dspName/effect.dsp"
        else
            faust -scn base_dsp -i -cn effect -a minimal-effect.cpp "$SRCDIR/$EFFECT" -o "$SRCDIR/$dspName/effect.h" || exit
        fi
    fi

done
